package com.ccc.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ccc.boot.exception.BusinessException;
import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.entity.RelUserRole;
import com.ccc.boot.system.mapper.RelUserRoleMapper;
import com.ccc.boot.system.pojo.RelateUserParam;
import com.ccc.boot.system.service.RelUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author superC
 * @version 0.1.0
 * @date 2022/4/2
 */
@Service
public class RelUserRoleServiceImpl implements RelUserRoleService {

    @Autowired
    private RelUserRoleMapper relUserRoleMapper;

    @Override
    public ResponseResult relateUsers(RelateUserParam param) {
        Long roleId = param.getRoleId();
        if (roleId == null) {
            throw new BusinessException(ResponseCode.PARAM_IS_BLANK);
        }
        List<Long> relUserIds = param.getUserIds();
        if (relUserIds == null || relUserIds.size() == 0) {
            throw new BusinessException(ResponseCode.PARAM_IS_BLANK);
        }

        // 去重
        List<Long> allIds = relUserRoleMapper.selectByRoleId(roleId);
        List<Long> newIds = relUserIds.stream().filter(id -> !allIds.contains(id)).collect(Collectors.toList());

        // 批量添加
        int result = relUserRoleMapper.insertBatch(roleId, newIds);
        if (result > 0) {
            return ResponseResult.success("关联用户成功");
        }
        return ResponseResult.failure("关联用户失败");
    }

    @Override
    public ResponseResult deleteUsers(RelateUserParam param) {
        Long roleId = param.getRoleId();
        if (roleId == null) {
            throw new BusinessException(ResponseCode.PARAM_IS_BLANK);
        }
        List<Long> relUserIds = param.getUserIds();
        if (relUserIds == null || relUserIds.size() == 0) {
            throw new BusinessException(ResponseCode.PARAM_IS_BLANK);
        }

        relUserIds.forEach(id -> {
            this.deleteUser(id, roleId);
        });
        return ResponseResult.success("取消关联用户成功");
    }

    @Override
    public boolean deleteUser(Long userId, Long roleId) {
       return relUserRoleMapper.deleteByKey(userId, roleId) > 0;
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        LambdaQueryWrapper<RelUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelUserRole::getUserId, userId);
        return delete(wrapper);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<RelUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RelUserRole::getRoleId, roleId);
        return delete(wrapper);
    }

    @Override
    public RelUserRole selectByKey(Long userId, Long roleId) {
        return relUserRoleMapper.selectByKey(userId, roleId);
    }

    private boolean delete(LambdaQueryWrapper<RelUserRole> wrapper) {
        Integer count = relUserRoleMapper.selectCount(wrapper);
        if (count > 0) {
            relUserRoleMapper.delete(wrapper);
        }
        return true;
    }

}